<template>
    <view @click.stop="diyClick()" v-if="link.type!='share'">
		<slot></slot>
    </view>
	<button  v-else class="button-container" style="padding: 0px;width:100%;height: 100%;" @click.stop="diyClick()" open-type='share' >
		<slot></slot>
	</button>
</template>

<script>
export default {
    data() {
        return {
            navigateUrl: '',
            form: {},
            userDefined: false,
        };
    },
    options: {
        multipleSlots: true // 在组件定义时的选项中启用多slot支持
    },
    /**
     * 组件的属性列表
     */
    props: {
        link: {
            type: Object,
            default: ''
        },
		type:{
			type: String,
			default: 'navigateTo'
		}
    },
    watch: {
        link: {
            handler: function (n) {
                if (n) {
                    this.init();
                }
            },

            immediate: true,
            deep: true
        },
        url: {
            handler: function (n) {
                if (n) {
                    this.init();
                }
            },

            immediate: true,
            deep: true
        }
    },
    beforeMount() {
		
        this.init();
    },
    /**
     * 组件的方法列表
     */
    methods: {
        init() {
			// 自动触发复制成功
			if(this.link && this.link.type=="copy" && this.link.value.type==2){
				uni.setClipboardData({
					data: this.link.value.value,
					success() {
						// #ifdef MP-ALIPAY
						uni.showToast({
							title:"复制成功",
							icon:'none'
						})
						// #endif
					}
				});
			}
        },

        callback(e) {
            const myEventDetail = { ...e.detail };

            this.$emit('callback', {
                detail: myEventDetail
            });
        },

        isJSON(str) {
            if (typeof str === 'string') {
                try {
                    const obj = JSON.parse(str);

                    if (typeof obj === 'object' && obj) {
                        return true;
                    } else {
                        return false;
                    }
                } catch (e) {
                    return false;
                }
            }
        },

        diyClick(e) {
			if(this.link=={}){
				retrun;
			}
			var link = this.link
			switch(link.type){
				case "link":
					this.toPath(link.value);
					break;
				case "goods":
					// 跳转商品页面
					this.toPath("/pages/mer/goods/details/details?id="+link.value)
					break;
				case "classify":
					// 跳转分类页面
					this.toPath(`/pages/mer/goods/list/list?name=${link.value.name}&type=${link.value.type}&id=${link.value.id}`);
					break;
				case "cards":
					// 跳转礼品卡页面
					this.toPath("/pages/mine/gift_purchase/gift_purchase?id="+link.value)
					break;
				case "copy":
					// 复制文本
					uni.setClipboardData({
						data: link.value.value,
						success() {
							// #ifdef MP-ALIPAY
							uni.showToast({
								title:"复制成功",
								icon:'none'
							})
							// #endif
						}
					});
					break;
				case "review":
					// #ifdef MP-WEIXIN
					// 重新组合内容
					if(link.value.type==1){
						var sources = [{
							url: link.value.value,
							type: 'image'
						}];
					}else{
						var sources = [{
							url: link.value.value,
							type: 'video'
						}];
					}
					wx.previewMedia({
						sources: sources,
						url:link.value.value
					});
					// #endif
					// #ifdef MP-ALIPAY
					if(link.value.type != "2"){
						uni.previewImage({
							urls: [link.value.value]
						});
					}
					// #endif
					break;
				case "phone":
					 uni.makePhoneCall({
						phoneNumber: link.value.value //仅为示例，并非真实的电话号码
					});
					break;
				case "jump_app":
					 this.$emit("jump");
					 uni.navigateToMiniProgram({
					   appId:link.value.value.appid,
					   path:link.value.value.path,
					   success(res) {
					     // 打开成功
					   }
					 })
					break;
				case "live":
					 wx.navigateTo({
					     url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${link.value.value.roomid}`
					 });
					break;
				// 跳转视频号
				case "video":
					if(link.value.type==1){
						// 判断打开视频号
						wx.openChannelsActivity({
							"finderUserName": link.value.value.id,
							"feedId":link.value.value.video_id,
						});
					}else{
						// 打开视频号直播
						wx.openChannelsLive({
							"finderUserName": link.value.value.live_id
						})
					}
					break;
					
			}
        },	

        toPath(url) {
			
			// 执行
			this.$emit("jump");
            uni[this.type]({
                url: url,
                complete(res) {
                    console.log(res);
                }
            });
        },
        navigatorClick() {}
    }
};
</script>
<style>
@import './qh-navigator.css';

</style>
